angular.module('Caltech').controller('userController', function ($scope, Restangular, ngTableParams, Roles, toaster, Constants, $modal) {
    'use strict';

    $scope.currentUserId = Constants.accountInfo.id;

    $scope.customerRole;

    $scope.search = function (role) {
        if ($scope.tableParams) {
            $scope.tableParams.reload();
        } else {
            $scope.tableParams = new ngTableParams({
                page: 1,
                count: 15
            }, {
                counts: [15, 20, 25],
                getData: function ($defer, params) {
                    Restangular.all('user')
                        .getList({
                            page: params.page(),
                            count: params.count(),
                            role: $scope.currentRole.id || undefined
                        }).then(function (users) {
                            params.total(users.total);

                            angular.forEach(users, function (user) {
                                user.role = {
                                    name: user.roleName,
                                    id: user.roleId
                                };

                                user.online = !!user.ip;
                            });

                            $defer.resolve(users);
                        });
                }
            });
        }
    };

    $scope.updateUser = function (user) {
        user.roleName = user.role.name;
        user.roleId = user.role.id;

        user.put().then(function () {
            toaster.pop('success', "Info", 'Updated');
        });
    };

    $scope.open = function (user) {
        var modalInstance = $modal.open({
            templateUrl: 'changePass.html',
            controller: function ($scope, $modalInstance) {
                $scope.ok = function (newPassword) {
                    $modalInstance.close(newPassword);
                };

                $scope.cancel = function () {
                    $modalInstance.dismiss('cancel');
                };
            },
        });

        modalInstance.result.then(function (password) {
            user.password = password;
            $scope.updateUser(user);
        });
    };

    $scope.openNewUserDialog = function () {
        $modal
            .open({
                template: '<iframe src="Home/Register" width="600px" height="700px"></iframe>',
                size: 'lg',
                resolve: {
                    items: function () {
                        return $scope.items;
                    }
                }
            })
            .result.catch(function () {
                $scope.search();
            });
    };

    $scope.roles = [];

    $scope.currentRole = {};

    $scope.deleteCustomerRole = function () {
        var customerIndex = -1;
        angular.forEach($scope.roles, function (role, index) {
            if (role.name == 'Customer') {
                customerIndex = index;
                $scope.customerRole = role;
                return;
            }
        });
        if (customerIndex != -1) {
            $scope.roles.splice(customerIndex, 1);
        }
    }

    $scope.$on('updateUsersOnlineCount', function (event, data) {
        $scope.tableParams.reload();
    });


    $scope.changeRole = function (role) {
        $scope.selectedRole = role;
        $scope.currentRole = $scope.selectedRole;
        $scope.search();
    };

    $scope.getCustomer = function () {
        $scope.currentRole = $scope.customerRole;
        $scope.search();
    };

    $scope.getRoles = function () {
        Restangular.all('user').customGETLIST('getRoles', {}).then(function (data) {
            $scope.roles = data;
            $scope.selectedRole = $scope.roles[0];
            $scope.currentRole = $scope.selectedRole;

            $scope.deleteCustomerRole();

            $scope.search();
        });
    };

    $scope.getRoles();

});